package ace.tool.generate.code.impl.resolver.ui.resolver;

import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.ui.model.bo.ResponseClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiProjectCommonInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiServerApiClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.bo.UiServiceClassInfo;
import ace.tool.generate.code.impl.resolver.ui.model.dto.UiClassFileInfo;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import lombok.Builder;
import lombok.RequiredArgsConstructor;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@Builder
@RequiredArgsConstructor
public class UiServiceClassInfoResolver {
  private final GenerateInput input;
  private final GenerateModelInput modelInput;
  private final UiServerApiClassInfo uiServerApiClassInfo;
  private final ResponseClassInfo responseClassInfo;
  private final UiProjectCommonInfo uiProjectCommonInfo;
  private final EntityModelManager entityModelManager = EntityModelManager.getInstance();

  public UiServiceClassInfo resolve() {
    String entityModelId = new EntityModelManager().getEntityModelId(input, modelInput);

    UiClassFileInfo classFileInfo =
        UiClassFileInfoResolver.builder()
            .className(this.modelInput.getName())
            .classNameSuffix(input.getUiServiceClassNameSuffix())
            .classPackagePrefix(input.getUiServiceClassPackagePrefix())
            .rootPackageFullName("/core/" + uiProjectCommonInfo.getProjectModuleCodeName())
            .sourceCodeRootDir(uiProjectCommonInfo.getSrcDir())
            .fileNameSuffix(".ts")
            .build()
            .resolve();

    return UiServiceClassInfo.builder()
        .entityModelId(entityModelId)
        .info(classFileInfo)
        .className(classFileInfo.getClassName())
        .entityResponseClassName(this.getEntityResponseClassName())
        .entityResponseClassFullName(this.getEntityResponseClassFullName())
        .serverApiClassName(this.uiServerApiClassInfo.getInfo().getUncapitalizeClassName())
        .serverApiClassFullName(this.uiServerApiClassInfo.getInfo().getUncapitalizeClassFullName())
        .build();
  }

  private String getEntityResponseClassFullName() {
    return this.responseClassInfo.getInfo().getClassFullName();
  }

  private String getEntityResponseClassName() {
    return this.responseClassInfo.getInfo().getClassName();
  }
}
